#include "dac7678.h"
#include <wiringPi.h>
#include <dac7678.h>

DECLARE(dac7678Setup);

IMPLEMENT(dac7678Setup) {
  SCOPE_OPEN();
  
  SET_ARGUMENT_NAME(0, pinBase);
  SET_ARGUMENT_NAME(1, i2cAddress);
  SET_ARGUMENT_NAME(2, vrefMode);
  
  CHECK_ARGUMENTS_LENGTH_EQUAL(3);
  
  CHECK_ARGUMENT_TYPE_INT32(0);
  CHECK_ARGUMENT_TYPE_INT32(1);
  CHECK_ARGUMENT_TYPE_UINT32(2);
  
  int pinBase = GET_ARGUMENT_AS_INT32(0);
  int i2cAddress = GET_ARGUMENT_AS_INT32(1);
  unsigned short vrefMode = GET_ARGUMENT_AS_UINT32(2);
  
  CHECK_ARGUMENT_IN_INTS(2, vrefMode, (DAC7678_VREF_MODE_STATIC_ON, 
                                      DAC7678_VREF_MODE_STATIC_OFF, 
                                      DAC7678_VREF_MODE_FLEXIBLE_ON, 
                                      DAC7678_VREF_MODE_FLEXIBLE_ALWAYS_ON,
                                      DAC7678_VREF_MODE_FLEXIBLE_ALWAYS_OFF));
  
  int res = ::dac7678Setup(pinBase, i2cAddress, vrefMode);
  
  SCOPE_CLOSE(INT32(res));
}

IMPLEMENT_EXPORT_INIT(dac7678) {
  EXPORT_FUNCTION(dac7678Setup);
  
  EXPORT_CONSTANT_INT(DAC7678_VREF_MODE_STATIC_ON);
  EXPORT_CONSTANT_INT(DAC7678_VREF_MODE_STATIC_OFF);
  EXPORT_CONSTANT_INT(DAC7678_VREF_MODE_FLEXIBLE_ON);
  EXPORT_CONSTANT_INT(DAC7678_VREF_MODE_FLEXIBLE_ALWAYS_ON);
  EXPORT_CONSTANT_INT(DAC7678_VREF_MODE_FLEXIBLE_ALWAYS_OFF);
}